<script setup lang="ts">
import { loginAPI } from '@/apis/user';
import { useUserStore } from '@/stores/user';
import { claearPassword, claearUser, getPassword, getUser, setPassword, setUser } from '@/utils/auth';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

interface FormData {
  username:string,
  password:string
}
// 表单数据
const formData = ref<FormData>({
  username:'demo',
  password:'Hmzs%001'
})

// 取到账号密码数据
 const res =  getUser()
 console.log('账号',res)

  const res1 =  getPassword()
  console.log('密码',res1)

  if(res && res1){
    formData.value.username = res
    formData.value.password = res1
  }else{
    console.log('账号密码为空')
  }

// 校验规则
const FormRules = ref<FormRules<FormData>>({
  username:[{ required:true,message:'请输入您的用户名',trigger:'blur'},
    { required:true,message:'请输入2-8位的用户名',min:2,max:8,trigger:'blur'}],
    password:[{ required:true,message:'请输入您的密码',trigger:'blur'},
    { required:true,message:'请输入6-11位的密码',min:6,max:11,trigger:'blur'}],
})

const ruleFormRef = ref<FormInstance>()
// 获取存储库
const useStore = useUserStore()
// 获取路由
const router = useRouter()
// 登录
 const onSubmit = () =>{
  ruleFormRef.value?.validate(async (valid)=>{
    if(valid){
      const res =  await loginAPI(formData.value)
        // 如果登录可以拿到token
         if(res.data.token){
          useStore.setToken(res.data.token)
          console.log('登录结果：',res)
          // 如果用户点击了记住我,存储账号密码
          if(isMember.value == true){
            setUser(formData.value.username)
            setPassword(formData.value.password)
          }else{
            // 清空账号密码
            console.log('清空账号密码')
             claearUser()
              claearPassword()
          }

          ElMessage({message:'登陆成功',type:"success"})

          // 跳转到登录页
          router.replace("/workbench")
         }
    }
  })
};

// 选中是否记住我 
 const isMember = ref(true)
</script>

<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="ruleFormRef" :model="formData" :rules="FormRules">
        <el-form-item  label="账号" prop="username">
          <el-input v-model ="formData.username" />
        </el-form-item>

        <el-form-item  label="密码" prop="password">
          <el-input v-model="formData.password" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model ="isMember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button  @click="onSubmit()" type="primary" class="login_btn">登录</el-button>
        </el-form-item>
      </el-form>
      {{ formData }}
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login_body {
  display: flex;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url("@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}

.box {
  margin: 200px 10% 0;
  flex: 1;

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }

  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }

    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }

    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}

.login_btn {
  width: 100%;
}
</style>
